package cpu.footprint.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cpu.footprint.R;
import cpu.footprint.util.DensityUtil;

/**
 * Created by Administrator on 2017/4/9.
 */
@ContentView(R.layout.activity_welcome)
public class WelcomeActivity extends Activity {
    @ViewInject(R.id.image_thumb)
    private ImageView image_thumb;//大拇指
    @ViewInject(R.id.image_index_finger)
    private ImageView image_index_finger;//食指
    @ViewInject(R.id.image_middle_finger)
    private ImageView image_middle_finger; //中指
    @ViewInject(R.id.image_ring_finger)
    private ImageView image_ring_finger;  //无名指
    @ViewInject(R.id.image_little_thumb)
    private ImageView image_little_thumb;//小拇指
    @ViewInject(R.id.image_foot)
    private ImageView image_foot;  //脚掌
    @ViewInject(R.id.image_text)
    private ImageView image_text;//文字
    private int Width;//屏幕宽度
    private int Height;//屏幕高度
    private ImageView[] welcomeImgs=new ImageView[7];
    private int[] imgSrc=new int[7];
    private int ImgIndex=0;
    private Animation animationAlpha;
    protected Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1: {
                    //切换图片显示
                    if(ImgIndex<7) {
                        // welcomeImgs[ImgIndex].setVisibility(View.VISIBLE);
                        welcomeImgs[ImgIndex].setAnimation(animationAlpha);
                        welcomeImgs[ImgIndex].setImageResource(imgSrc[ImgIndex]);
                        ImgIndex++;
                    }else{
                        //跳转Activity
                        Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(0,R.anim.fade);
                        WelcomeActivity.this.finish();
                    }
                }break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        Width=DensityUtil.getDisplayWidth(this);
        Height=DensityUtil.getDisplayHeight(this);
        InitView();//初始化布局
        StartImgThread();
        animationAlpha = new AlphaAnimation(0.0f, 1.0f);
        animationAlpha.setDuration(500);
    }

    /**
     * 建立图片切换线程
     * @return
     */
    private void StartImgThread() {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for(int i=0;i<7;i++){
                    try {
                        mHandler.sendEmptyMessage(1);
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(1);
            }
        }) ;
        t.start();
    }

    /**
     * 初始化图片位置
     */
    private void InitView() {
        SetImageLocation(0.28,0.2,0.085,0.055,image_thumb);
        SetImageLocation(0.38,0.187,0.05,0.05,image_index_finger);
        SetImageLocation(0.46,0.187,0.04,0.05,image_middle_finger);
        SetImageLocation(0.51,0.187,0.05,0.05,image_ring_finger);
        SetImageLocation(0.57,0.213,0.05,0.05,image_little_thumb);
        SetImageLocation(0.355,0.26,0.3,0.2,image_foot);
        SetImageLocation(0.2,0.7,0.6,0.05,image_text);
        welcomeImgs[0]=image_thumb;
        welcomeImgs[1]=image_index_finger;
        welcomeImgs[2]=image_middle_finger;
        welcomeImgs[3]=image_ring_finger;
        welcomeImgs[4]=image_little_thumb;
        welcomeImgs[5]=image_foot;
        welcomeImgs[6]=image_text;
        imgSrc[0]=R.mipmap.thumb;
        imgSrc[1]=R.mipmap.index_finger;
        imgSrc[2]=R.mipmap.middle_finger;
        imgSrc[3]=R.mipmap.ring_finger;
        imgSrc[4]=R.mipmap.little_thumb;
        imgSrc[5]=R.mipmap.foot;
        imgSrc[6]=R.mipmap.welcome_text;
    }

    /**
     * 设置图片位置
     * @param b1
     * @param b2
     * @param b3
     * @param b4
     * @param image
     */
    private void SetImageLocation(double b1, double b2, double b3, double b4, ImageView image) {
        int x1;
        int y1;
        int w1;
        int h1;
        x1 = (int) (Width * b1);
        y1 = (int) (Height * b2);
        w1 = (int) (Width * b3);
        h1 = (int) (Height * b4);
        AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(w1, h1, x1, y1);
        image.setLayoutParams(layoutParams);
    }

    /**
     * 重写后退函数
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            WelcomeActivity.this.finish();
            overridePendingTransition(0,R.anim.fade);
            android.os.Process
                    .killProcess(android.os.Process
                            .myPid());
            System.exit(0);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}
